//
//  HomePageViewController.m
//  Balance
//
//  Created by Sophia Tsay on 5/31/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "HomePageViewController.h"
#import "TopDelinquentCustomersViewController.h"

@interface HomePageViewController ()
@property (strong, nonatomic) IBOutlet UITableView *notificationTableView;

@end

@implementation HomePageViewController
@synthesize delinquentCustomersView = _delinquentCustomersView;
@synthesize notificationTableView = _notificationTableView;

- (void)setNotificationTableView:(UITableView *)notificationTableView
{
    _notificationTableView = notificationTableView;
    self.notificationTableView.backgroundView = nil;
    self.notificationTableView.backgroundColor = [UIColor whiteColor];
    self.notificationTableView.delegate = self;
    self.notificationTableView.dataSource = self;
    //UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.notificationTableView.frame.origin.x, self.notificationTableView.frame.origin.y, self.notificationTableView.frame.size.width, 36)];
    //tableHeaderLabel.text = @"";
    //[self.notificationTableView.tableHeaderView addSubview:tableHeaderLabel];
}

/*
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.delinquentCustomersView reloadData];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [self setNotificationTableView:nil];
    [self setDelinquentCustomersView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}

#pragma mark - Table view data source
#define notifications [NSArray arrayWithObjects:@"Credit Hold", @"Order Hold", @"Approval for Dispute", @"Overdue Promise", @"Pre-delinquency", @"Strategy Work Items", @"Customer Reassignments",nil]

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if (section == 1) {
        return @"Top Delinquent Customers";
    } else if (section == 0) {
        return @"Notifications";
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRows = 0;
    if (section == 1) {
        //numberOfRows = [topDelinquentCustomers count];
        numberOfRows = 10;
    } else if (section == 0) {
        numberOfRows = [notifications count];
    }
    return numberOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *TopCellIdentifier = @"Top Delinquent Customers";
    static NSString *NotificationsCellIdentifier = @"Notifications";
    UITableViewCell *cell;

    if (indexPath.section == 1) {
        
        cell = [tableView dequeueReusableCellWithIdentifier:TopCellIdentifier];    
        cell.textLabel.text = [topDelinquentCustomers objectAtIndex:indexPath.row];
        //cell.detailTextLabel.text = @"Balance: $10,000.21 Overdue:$6,000";
        
    } else if (indexPath.section == 0) {
        
        cell = [tableView dequeueReusableCellWithIdentifier:NotificationsCellIdentifier];    
        cell.textLabel.text = [notifications objectAtIndex:indexPath.row];
        //cell.detailTextLabel.text = @"Balance: $10,000.21 Overdue:$6,000";
        
    }
    
    //cell.backgroundColor = [UIColor darkGrayColor];
    //cell.textLabel.textColor = [UIColor lightGrayColor];
    //cell.detailTextLabel.textColor = [UIColor whiteColor];
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 2;
    [cell.textLabel sizeToFit];
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

#pragma mark - Spread View Datasource

//no of columns per column section
- (NSInteger)spreadView:(MDSpreadView *)aSpreadView numberOfColumnsInSection:(NSInteger)section
{
    //Exclude Collector column for Collector tab.
    return [delinquentCustomerHeaders count]-1;
}

//no or rows per row section
- (NSInteger)spreadView:(MDSpreadView *)aSpreadView numberOfRowsInSection:(NSInteger)section
{
    //return [topDelinquentCustomers count];
    return [delinquentCustomerValues count];
    //return 6;
}

//no of column sections
- (NSInteger)numberOfColumnSectionsInSpreadView:(MDSpreadView *)aSpreadView
{
    //return 4;
    return 1;
}

// no of row sections
- (NSInteger)numberOfRowSectionsInSpreadView:(MDSpreadView *)aSpreadView
{
    //return 10;
    return 1;
}

#pragma mark Heights
// Comment these out to use normal values (see MDSpreadView.h)
/*
 - (CGFloat)spreadView:(MDSpreadView *)aSpreadView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 return 25+indexPath.row;
 }
 
 - (CGFloat)spreadView:(MDSpreadView *)aSpreadView heightForRowHeaderInSection:(NSInteger)rowSection
 {
 //    if (rowSection == 2) return 0; // uncomment to hide this header!
 
 return 22+rowSection*10;
 }
 */ 

- (CGFloat)spreadView:(MDSpreadView *)aSpreadView widthForColumnAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (indexPath.row) {
        case 0:  //customer
            return 160;
            break;
        case 1:  //Total Delinquent
            return 130;
            break;
        case 2:  //Open Receivables
            return 170;
            break;
        case 3:  //DSO
            return 80;
            break;
            
        default:
            return 120;
            break;
    }
    
    //return 80;
}

/* 
 - (CGFloat)spreadView:(MDSpreadView *)aSpreadView widthForColumnHeaderInSection:(NSInteger)columnSection
 {
 //    if (columnSection == 2) return 0; // uncomment to hide this header!
 
 return 110+columnSection*50;
 }
 */
#pragma Cells
- (MDSpreadViewCell *)spreadView:(MDSpreadView *)aSpreadView cellForRowAtIndexPath:(NSIndexPath *)rowPath forColumnAtIndexPath:(NSIndexPath *)columnPath
{
    static NSString *cellIdentifier = @"Cell";
    
    MDSpreadViewCell *cell = [aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MDSpreadViewCell alloc] initWithStyle:MDSpreadViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    //cell.textLabel.text = [NSString stringWithFormat:@"Test Row %d-%d (%d-%d)", rowPath.section+1, rowPath.row+1, columnPath.section+1, columnPath.row+1];
    // cell.textLabel.text = [trxnRecordArray objectAtIndex:columnPath.row];
    /*    
     switch (columnPath.row) {
     
     case 0:  //transaction
     trxnNumber = [[trxnRecordArray objectAtIndex:columnPath.row] intValue] +(arc4random()%10) + 10 * rowPath.column;
     cell.textLabel.text = [NSString stringWithFormat:@"%d", trxnNumber];
     cell.textLabel.textAlignment = UITextAlignmentLeft;
     break; 
     
     case 1:  //original amount
     originalAmount = [[trxnRecordArray objectAtIndex:columnPath.row] intValue] +(arc4random()%10000000);
     cell.textLabel.text = [currencyFormatter stringFromNumber:[NSNumber numberWithInt:originalAmount]];
     cell.textLabel.textAlignment = UITextAlignmentRight;
     break;
     case 2:  //remaining amount
     remainingAmount = (arc4random()%1000000);
     cell.textLabel.text = [currencyFormatter stringFromNumber:[NSNumber numberWithInt:remainingAmount]];
     cell.textLabel.textAlignment = UITextAlignmentRight;
     break;
     case 3:  //days late
     daysLate = (arc4random()%150);
     cell.textLabel.text = [NSString stringWithFormat:@"%d", daysLate];
     cell.textLabel.textAlignment = UITextAlignmentRight;
     break;
     default:
     cell.textLabel.text = [trxnRecordArray objectAtIndex:columnPath.row];
     cell.textLabel.textAlignment = UITextAlignmentLeft;
     break;
     }
     */
    /*   
     if (columnPath.row == 0) {
     cell.textLabel.text = [topDelinquentCustomers objectAtIndex:rowPath.row];
     } else {
     cell.textLabel.text = [detailValueList objectAtIndex:columnPath.row-1];
     }
     */
    
    NSArray *currentCustomer = [delinquentCustomerValues objectAtIndex:rowPath.row];
    
    cell.textLabel.text = [currentCustomer objectAtIndex:columnPath.row];        
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    if (columnPath.row == 0 || columnPath.row == 18 || columnPath.row == 19) {
        cell.textLabel.textAlignment = UITextAlignmentLeft;
    } else {
        cell.textLabel.textAlignment = UITextAlignmentRight;
    }
    return cell;
}


//There are 3 types of header cells and the following 3 methods creating these cells for each type.
//CornerHeaderCell, RowHeaderCell,ColumnHeaderCell.
- (MDSpreadViewCell *)spreadView:(MDSpreadView *)aSpreadView cellForHeaderInRowSection:(NSInteger)rowSection forColumnSection:(NSInteger)columnSection
{
    static NSString *cellIdentifier = @"CornerHeaderCell";
    
    MDSpreadViewHeaderCell *cell = (MDSpreadViewHeaderCell *)[aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MDSpreadViewHeaderCell alloc] initWithStyle:MDSpreadViewHeaderCellStyleCorner reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"Cor %d-%d", columnSection+1, rowSection+1];
    
    return cell;
}
//this is actually the header for each column
- (MDSpreadViewCell *)spreadView:(MDSpreadView *)aSpreadView cellForHeaderInRowSection:(NSInteger)section forColumnAtIndexPath:(NSIndexPath *)columnPath
{
    static NSString *cellIdentifier = @"RowHeaderCell";
    
    MDSpreadViewHeaderCell *cell = (MDSpreadViewHeaderCell *)[aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MDSpreadViewHeaderCell alloc] initWithStyle:MDSpreadViewHeaderCellStyleRow reuseIdentifier:cellIdentifier];
    }
    
    //cell.textLabel.text = [NSString stringWithFormat:@"Row Header %d (%d-%d)", section+1, columnPath.section+1, columnPath.row+1];
    /*
     if (columnPath.row == 0) {
     cell.textLabel.text = @"Customer Name";
     } else {
     cell.textLabel.text = [detailTitleList objectAtIndex:columnPath.row-1];
     }
     */    
    cell.textLabel.text = [delinquentCustomerHeaders objectAtIndex:columnPath.row];    
    
    cell.textLabel.frame = cell.frame;
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    if (columnPath.row == 0 || columnPath.row == 18 || columnPath.row == 19) {
        cell.textLabel.textAlignment = UITextAlignmentLeft;
    } else {
        cell.textLabel.textAlignment = UITextAlignmentRight;
    }
    
    return cell;
}

//this is actually the header for each row
- (MDSpreadViewCell *)spreadView:(MDSpreadView *)aSpreadView cellForHeaderInColumnSection:(NSInteger)section forRowAtIndexPath:(NSIndexPath *)rowPath
{
    static NSString *cellIdentifier = @"ColumnHeaderCell";
    
    MDSpreadViewHeaderCell *cell = (MDSpreadViewHeaderCell *)[aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MDSpreadViewHeaderCell alloc] initWithStyle:MDSpreadViewHeaderCellStyleColumn reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%d (%d-%d)", section+1, rowPath.section+1, rowPath.row+1];
    
    return cell;
}

@end
